package cn.wolfcode.web.service.impl;

import cn.wolfcode.web.domain.Role;
import cn.wolfcode.web.mapper.RoleMapper;
import cn.wolfcode.web.page.PageResult;
import cn.wolfcode.web.query.QueryObject;
import cn.wolfcode.web.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public Role get(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> listAll() {

        List<Role> roles = roleMapper.selectAll();
        return roles;
    }

    @Override
    public void update(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }



    @Override
    public PageResult query(QueryObject qo) {

        int rows = roleMapper.queryForCount(qo);
        if (rows==0) {
            //        (int currentPage,int  pageSize,int  rows,List<?>  result)
            return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),0, Collections.emptyList());
        }

        List<Role> roleList = roleMapper.queryForList(qo);
        return  new PageResult(qo.getCurrentPage(),qo.getPageSize(),rows,roleList);
    }

    @Override
    public List<Role> selectById(Long id) {

        return roleMapper.selectById(id);
    }

}
